package asips

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ AccountAsipsModel = (*customAccountAsipsModel)(nil)

type (
	// AccountAsipsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customAccountAsipsModel.
	AccountAsipsModel interface {
		accountAsipsModel
		UpdateOneField(ctx context.Context, data *AccountAsips, key string, value int64) error
	}

	customAccountAsipsModel struct {
		*defaultAccountAsipsModel
	}
)

func (m *customAccountAsipsModel) UpdateOneField(ctx context.Context, data *AccountAsips, key string, value int64) error {
	accountAsipsIdKey := fmt.Sprintf("%s%v", cacheAccountAsipsIdPrefix, data.Id)
	accountAsipsUsernameKey := fmt.Sprintf("%s%v", cacheAccountAsipsUsernamePrefix, data.Username)
	_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fmt.Sprintf(" `%s` = ?", key))
		return conn.ExecCtx(ctx, query, value, data.Id)
	}, accountAsipsIdKey, accountAsipsUsernameKey)
	return err
}

// NewAccountAsipsModel returns a model for the database table.
func NewAccountAsipsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) AccountAsipsModel {
	return &customAccountAsipsModel{
		defaultAccountAsipsModel: newAccountAsipsModel(conn, c, opts...),
	}
}
